package com.be.beadmin.project.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.ProjectPhaseDto;
import com.be.beadmin.project.dto.output.ProjectPhaseEx;
import com.be.beadmin.project.service.ProjectPhaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-06-13
**/
@RestController
public class ProjectPhaseController {

    @Autowired
    private  ProjectPhaseService projectPhaseService;


    @PostMapping(value = "/design/getProjectPhaseByPage")
    public ResultModel getProjectPhaseByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(projectPhaseService.getProjectPhaseByPage(pageEntity)) ;
    }

    @PostMapping("/design/addProjectPhase")
    public ResultModel addProjectPhase(@RequestBody ProjectPhaseDto projectPhaseDto){
        projectPhaseService.addProjectPhase(projectPhaseDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/design/delProjectPhase")
    public ResultModel delProjectPhase(@RequestBody ProjectPhaseDto projectPhaseDto){
        projectPhaseService.delProjectPhase(projectPhaseDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/design/updateProjectPhase")
    public ResultModel updateProjectPhase(@RequestBody ProjectPhaseDto projectPhaseDto){
        projectPhaseService.updateProjectPhase(projectPhaseDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/design/exportProjectPhase")
    public void exportProjectPhase(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ProjectPhaseEx> projectPhasePage = projectPhaseService.getProjectPhaseByPage(pageEntity);
        List<ProjectPhaseEx> projectPhaseExes = projectPhasePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ProjectPhase接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProjectPhaseEx.class).sheet("sheet1").doWrite(projectPhaseExes);
    }
}